﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;

namespace TI_EntityLib.TiEntityObjects.Kladr
{
    public class KladrItemsRoot 
    {   
        public List<KladrItem> Items{ get; set; }

        public  ICollection GetCollection()
        {
            return Items;
        }

        public KladrItemsRoot(XmlNode node)
        {
            string  s=string.Empty;
            this.Items = new List<KladrItem>();
            KladrItem newItem = null;

            foreach (XmlNode i in node.ChildNodes)
            {
                s = i.Attributes["Code"].Value;
                newItem = new KladrItem();
                
                newItem.Code = GetValue( i,"Code");
                newItem.FullName = GetValue(i, "FullName");
                newItem.Gninb = GetValue(i, "Gninb");
                newItem.Index = GetValue(i, "Index");
                newItem.Level = Convert.ToInt16(GetValue(i, "Level"));
                newItem.Level1 = GetValue(i, "Level1");
                newItem.Level2 = GetValue(i, "Level2");
                newItem.Level3 = GetValue(i, "Level3");
                newItem.Level4 = GetValue(i, "Level4");
                newItem.Name = GetValue(i, "Name");
                newItem.Socr = GetValue(i, "Socr");
                newItem.ParentId = GetValue(i, "ParentId");                
                newItem.SocrName = GetValue(i, "SocrName");
                this.Items.Add(newItem);
            }
        }

        public static string GetValue(XmlNode n,string AttName) {
            
            if (null == n.Attributes[AttName])
                return null;

            return n.Attributes[AttName].Value;
        }
        
    }

        public class KladrItem
        {
            [XmlElement("SocrName")]
            public string  SocrName { get; set; }

            [XmlElement("Level")]
            public int Level { get; set; }

            [XmlElement("Level1")]
            public string  Level1 { get; set; }

            [XmlElement("Level2")]
            public string  Level2 { get; set; }

            [XmlElement("Level3")]
            public string Level3 { get; set; }

            [XmlElement("Level4")]
            public string Level4 { get; set; }

            [XmlElement("Name")]
            public string Name{ get; set; }

            [XmlElement("Socr")]
            public string Socr { get; set; }

            [XmlElement("Code")]
            public string Code { get; set; }

            [XmlElement("Index")]
            public string Index { get; set; }

            [XmlElement("Gninb")]
            public string Gninb { get; set; }

            [XmlElement("FullName")]
            public string FullName { get; set; }

            [XmlElement("ParentId")]
            public string ParentId { get; set; }
            
            public override string ToString()
            {
                return Helper.Json<KladrItem>.NewtonSerializeObject(this);
            }

        }
    }

    
    
